```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高并发限流算法解析 | 令牌桶与漏桶实现</title>
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #7c3aed;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .first-letter {
            font-size: 3.5em;
            line-height: 0.8;
            float: left;
            margin-right: 0.1em;
            color: #4f46e5;
            font-weight: 700;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-24 md:py-32">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">高并发限流算法解析</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8 font-serif">令牌桶与漏桶：保障系统稳定的关键技术</p>
                <div class="inline-flex items-center space-x-4">
                    <span class="flex items-center"><i class="fas fa-clock mr-2"></i> 时间复杂度: O(1)</span>
                    <span class="flex items-center"><i class="fas fa-memory mr-2"></i> 空间复杂度: O(1)</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="prose prose-lg max-w-none">
                <p><span class="first-letter">在</span>高并发系统设计中，限流是保障系统稳定的重要手段。当突发流量超出系统处理能力时，合理的限流算法能够避免系统崩溃，保证核心业务正常运行。本文将深入解析两种经典限流算法：令牌桶和漏桶，并通过可视化方式展示其工作原理。</p>
            </div>
        </section>

        <!-- Algorithm Comparison -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center font-serif">限流算法对比</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Token Bucket Card -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-12 h-12 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-4">
                                <i class="fas fa-coins text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">令牌桶算法</h3>
                        </div>
                        <p class="text-gray-600 mb-4">以固定速率生成令牌，请求需要消耗令牌。当令牌耗尽时，新的请求将被限制。</p>
                        <ul class="space-y-2 mb-6">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>允许突发流量（桶中有足够令牌时）</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>平滑输出，限制平均速率</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>实现简单，效率高</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <!-- Leaky Bucket Card -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden card-hover">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="w-12 h-12 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-4">
                                <i class="fas fa-faucet text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold">漏桶算法</h3>
                        </div>
                        <p class="text-gray-600 mb-4">请求以固定速率流出，当桶满时新的请求将被丢弃或排队等待。</p>
                        <ul class="space-y-2 mb-6">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>严格控制请求速率，绝对平滑</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>适合稳定流量的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>可以缓冲突发请求</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center font-serif">算法原理可视化</h2>
            
            <div class="bg-white rounded-xl shadow-lg overflow-hidden p-6 mb-8">
                <div class="mermaid">
                    graph TD
                    subgraph 令牌桶算法
                    A[令牌生成器] -->|固定速率| B[令牌桶]
                    B -->|消耗令牌| C[请求处理]
                    end
                    
                    subgraph 漏桶算法
                    D[请求输入] --> E[漏桶]
                    E -->|固定速率| F[请求处理]
                    end
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-indigo-50 rounded-lg p-6">
                    <h3 class="text-xl font-bold mb-4 text-indigo-800">令牌桶工作流程</h3>
                    <ol class="list-decimal pl-5 space-y-2 text-indigo-900">
                        <li>系统以固定速率向桶中添加令牌</li>
                        <li>请求到达时尝试从桶中获取令牌</li>
                        <li>获取成功则处理请求，否则拒绝</li>
                        <li>桶中令牌数不超过容量上限</li>
                    </ol>
                </div>
                
                <div class="bg-purple-50 rounded-lg p-6">
                    <h3 class="text-xl font-bold mb-4 text-purple-800">漏桶工作流程</h3>
                    <ol class="list-decimal pl-5 space-y-2 text-purple-900">
                        <li>请求到达时进入漏桶队列</li>
                        <li>漏桶以固定速率释放请求进行处理</li>
                        <li>当桶满时，新请求被丢弃或排队</li>
                        <li>输出速率恒定，不受输入速率影响</li>
                    </ol>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center font-serif">代码实现示例</h2>
            
            <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                <div class="flex items-center justify-between bg-gray-800 px-6 py-3">
                    <div class="flex items-center space-x-2">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <div class="text-gray-300 text-sm">Python - 令牌桶实现</div>
                </div>
                <div class="code-block p-6 overflow-x-auto">
                    <pre class="text-gray-200"><code>class TokenBucket:
    def __init__(self, capacity, rate):
        self.capacity = capacity  # 桶的容量
        self.tokens = capacity    # 当前令牌数量
        self.rate = rate          # 令牌生成速率(tokens/second)
        self.last_refill = time.time()  # 上次填充时间
    
    def allow_request(self):
        now = time.time()
        # 计算新增令牌数
        self.tokens = min(
            self.capacity, 
            self.tokens + (now - self.last_refill) * self.rate
        )
        self.last_refill = now
        
        # 检查是否有足够令牌
        if self.tokens >= 1:
            self.tokens -= 1
            return True  # 允许请求
        return False     # 拒绝请求</code></pre>
                </div>
            </div>
        </section>

        <!-- Application Scenarios -->
        <section>
            <h2 class="text-3xl font-bold mb-8 text-center font-serif">应用场景</h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-indigo-500">
                    <div class="text-indigo-500 mb-3">
                        <i class="fas fa-server text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">API限流</h3>
                    <p class="text-gray-600">保护后端服务不被突发流量击垮，确保服务稳定性。</p>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-purple-500">
                    <div class="text-purple-500 mb-3">
                        <i class="fas fa-user-shield text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">防DDoS攻击</h3>
                    <p class="text-gray-600">限制单一IP的请求频率，防止恶意攻击。</p>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-blue-500">
                    <div class="text-blue-500 mb-3">
                        <i class="fas fa-tasks text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">任务队列控制</h3>
                    <p class="text-gray-600">控制任务处理速率，避免资源耗尽。</p>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```